package com.goudan.demo.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DateUtil {

    public static final String DATE_FROMAT_YYYYMMDD = "yyyyMMdd";
    public static final String DATE_FROMAT_YYMMDD = "yyMMdd";
    public static final String TIME_FORMAT_HHMMSS = "HHmmss";
    public static final String YYYY_MM_FORMAT = "yyyy-MM";
    public static final String YYYY_MM_DD_FORMAT = "yyyy-MM-dd";
    public static final String YYYY$MM$DD_FORMAT = "yyyy/MM/dd";
    public static final String YYYY$MM$DD$hh$mm$ss_FORMAT = "yyyy/MM/dd HH:mm:ss";
    public static final String YYYY_MM_DD$hh$mm$ss_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD$hh$mm$ss$SSS_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String YYYY_MM_DD$hh$mm$ss_SSS_FORMAT = "dd-MMM-yyyy HH:mm:ss:SSS";
    public static final String hh$mm$ss_FORMAT = "HH:mm:ss";
    public static final String hhmmssSSS_FORMAT = "HHmmssSSS";
    public static final String YYYY_MM_DDT$hh$mm$ss_SSS_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
    public static final String DATE_FROMAT_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    private static final Map<String, ThreadLocal<SimpleDateFormat>> safe_tl_map = new ConcurrentHashMap();
    private static final Object obj_lock = new Object();

    public static String getCurDate(String strFormat) {
        Date currentDate = new Date();
        return getSimpleDateFormat(strFormat).format(currentDate);
    }

    private static SimpleDateFormat getSimpleDateFormat(final String pattern) {
        ThreadLocal<SimpleDateFormat> tl = safe_tl_map.get(pattern);
        if (tl == null) {
            synchronized (obj_lock) {
                tl = safe_tl_map.get(pattern);
                if (tl == null) {
                    tl = new ThreadLocal<SimpleDateFormat>() {
                        protected SimpleDateFormat initialValue() {
                            return new SimpleDateFormat(pattern);
                        }
                    };
                    safe_tl_map.put(pattern, tl);
                }
            }
        }
        return tl.get();
    }
}
